<template>
  <div class="todoContain">
    <h1>备忘录</h1>

    <input
      type="text"
      v-model="todo"
      @keyup.enter="addTodo"
      placeholder="请输入备忘内容，按回车添加"
    />

    <ul v-if="todos.length > 0">
      <li v-for="(todo, index) in todos" :key="index">
        <span>{{ todo }}</span>
        <button @click="removeTodo(index)">×</button>
      </li>
    </ul>
    <div v-else class="empty">暂无备忘事项，请添加...</div>

    <div class="footer">
      <span>总计: {{ todos.length }} 条</span>
      <button @click="clearAll" v-show="todos.length">清空全部</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      todo: "",
      todos: [],
    };
  },
  methods: {
    addTodo() {
      if (this.todo.trim() === "") return;

      this.todos.push(this.todo);
      this.todo = "";
    },

    removeTodo(index) {
      this.todos.splice(index, 1);
    },

    clearAll() {
      this.todos = [];
    },
  },
};
</script>

<style scoped>
.todoContain {
  width: 500px;
  margin: 0 auto;
  padding: 20px;
  background: #f5f5f5;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h1 {
  text-align: center;
  color: #333;
}

input {
  outline: none;
  width: 480px;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  margin-bottom: 5px;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

ul li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  background: white;
  margin-bottom: 8px;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

ul li button {
  background: #ff6b6b;
  color: white;
  border: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  text-align: center;
  line-height: 24px;
}

.empty {
  text-align: center;
  padding: 20px;
  color: #999;
  background: white;
  border-radius: 4px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  padding-top: 10px;
  border-top: 1px solid #eee;
}

.footer button {
  padding: 5px 10px;
  background: #ff6b6b;
  color: white;
  border: none;
  border-radius: 4px;
}
</style>